﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using CRMMis;
using System.Data;

public partial class CRM_Manager_ERP_TypeManage : BasePage
{
    private static string clickNodeId = "";
    private static readonly string rootId = "0";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BuildTreeRoot();
        }
    }

    //构建树根节点
    private void BuildTreeRoot()
    {
        TreePanel1.Root.Clear();
        Ext.Net.AsyncTreeNode root = new AsyncTreeNode(rootId, "商品类别分类");
        root.Draggable = false;
        root.Expanded = true;
        this.TreePanel1.Root.Add(root);

        TreePanel1.SelectNode(rootId);
        NodeClick(rootId);
    }

    protected void GetPY(object sender, DirectEventArgs e)
    {
        string Name = exttxtf_Type_NAME.Text.Trim();
        if (!string.IsNullOrEmpty(Name))
        {
            exttxtf_Type_pym.Text = GetPinyinByName(Name);
        }
    }

    [DirectMethod]
    public string NodeLoad(string nodeID)
    {
        Ext.Net.TreeNodeCollection nodes = new Ext.Net.TreeNodeCollection();
        if (!string.IsNullOrEmpty(nodeID))
        {
            DataTable TreeData = CRMMis.BLL.ERP_Type.GetListSub(" DEL_FLAG =0 and ltrim(rtrim(PARENT_ID)) = '" + nodeID.Trim() + "' ", "Type_Id").Tables[0];
            foreach (DataRow row in TreeData.Rows)
            {
                AsyncTreeNode asyncNode = new AsyncTreeNode();
                asyncNode.Text = row["Type_NAME"].ToString();
                asyncNode.NodeID = row["Type_Id"].ToString();
                if (row["leaf"].ToString() == "1")
                    asyncNode.Leaf = false;
                else
                    asyncNode.Leaf = true;
                nodes.Add(asyncNode);
            }
        }
        string a = nodes.ToJson();
        return nodes.ToJson();
    }

    //单击树节点
    [DirectMethod]
    public void NodeClick(string NodeId)
    {
        clickNodeId = NodeId;
        GridPanel1.Reload();
    }

    [DirectMethod]
    public string MoveNode(string nodeId, string parentId)
    {
        string tmp = "";
        try
        {
            CRMMis.Model.ERP_Type proModel = CRMMis.BLL.ERP_Type.GetModel(int.Parse(nodeId));
            proModel.PARENT_ID = Convert.ToInt32(parentId);
            CRMMis.BLL.ERP_Type.Update(proModel);
            NodeClick(rootId);
        }
        catch (Exception ex)
        {
            tmp = ex.Message;
        }
        return tmp;
    }

    #region Grid相关

    //加载列表
    private void BindGrid()
    {
        DataTable GridData = CRMMis.BLL.ERP_Type.GetList(GetQryString()).Tables[0];
        this.Store1.DataSource = GridData;
        this.Store1.DataBind();
    }

    private string GetQryString()
    {
        string result = "1=1 ";
        result += " and PARENT_ID = '" + clickNodeId.Trim() + "' ";
        if (exttxtf_user_name.Text != "")
            result += " and Type_Name like '%" + exttxtf_user_name.Text.Trim() + "%'";
        return result;
    }

    protected void OnStoreRefreshData(object sender, StoreRefreshDataEventArgs e)
    {
        BindGrid();
    }

    protected void extBtn_Find_Click(object sender, DirectEventArgs e)
    {
        BindGrid();
    }

    protected void extBtn_Refresh_Click(object sender, DirectEventArgs e)
    {
        BindGrid();
    }

    #endregion

    #region 按钮相关

    [DirectMethod]
    public string ShowData(string Obj_ID)
    {
        string tmp = "";
        try
        {
            DataTable dt = CRMMis.BLL.ERP_Type.GetList(" Type_Id = " + Obj_ID).Tables[0];
            if (dt.Rows.Count > 0)
            {
                exttxtf_Type_ID.Text = Obj_ID;
                exttxtf_inner_no.Text = dt.Rows[0]["INNER_NO"].ToString();
                exttxtf_Type_NAME.Text = dt.Rows[0]["Type_NAME"].ToString();
                exttxtf_Type_pym.Text = dt.Rows[0]["Type_PYM"].ToString();
                exttxtf_remark.Text = dt.Rows[0]["REMARK"].ToString();
                extHid_coboDeptId.Text = dt.Rows[0]["PARENT_ID"].ToString();

                ViewState["INNER_NO"] = dt.Rows[0]["INNER_NO"].ToString();

                if (dt.Rows[0]["PARENT_ID"].ToString() == rootId)
                    exttxtf_parent_id.Text = "商品分类";
                else
                {
                    DataTable dt_dept = CRMMis.BLL.ERP_Type.GetListSub("Type_Id = " + dt.Rows[0]["PARENT_ID"].ToString(), "Type_Id").Tables[0];
                    if (dt_dept.Rows.Count > 0)
                        exttxtf_parent_id.Text = dt_dept.Rows[0]["Type_NAME"].ToString();
                }
            }
        }
        catch (Exception ex)
        {
            tmp = ex.Message;
        }
        return tmp;
    }

    [DirectMethod]
    public string SaveData(string Obj_ID)
    {
        string tmp = "";
        try
        {
            CRMMis.Model.ERP_Type proModel = new CRMMis.Model.ERP_Type();
            if (Obj_ID != "Add")
                proModel = CRMMis.BLL.ERP_Type.GetModel(int.Parse(Obj_ID));
            else
            {
                proModel.Type_ID = CRMMis.BLL.ERP_Type.GetNewID();
                var parentModel = CRMMis.BLL.ERP_Type.GetModel(int.Parse(extHid_coboDeptId.Text.Trim()));
                var ParentInnerNo = "";
                if (parentModel == null)
                    ParentInnerNo = "00";
                else
                    ParentInnerNo = parentModel.INNER_NO;
                proModel.INNER_NO = CRMMis.BLL.ERP_Type.GetNewParentId(ParentInnerNo.Trim());
            }
            proModel.Type_NAME = exttxtf_Type_NAME.Text;
            proModel.Type_PYM = exttxtf_Type_pym.Text.Trim();
            proModel.PARENT_ID = Convert.ToInt32(extHid_coboDeptId.Text);
            proModel.REMARK = exttxtf_remark.Text;
            if (Obj_ID != "Add")
                CRMMis.BLL.ERP_Type.Update(proModel);
            else
            {
                CRMMis.BLL.ERP_Type.Add(proModel);
                extHid_Obj_ID.Text = proModel.Type_ID.ToString();
            }

            BuildTreeRoot();
            TreePanel1.DoLayout();
        }
        catch (Exception ex)
        {
            tmp = ex.Message;
        }
        return tmp;
    }

    protected void DelUser(object sender, DirectEventArgs e)
    {
        int nCount = 0;
        RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
        nCount = sm.SelectedRows.Count;
        if (nCount == 0)
        {
            X.Msg.Alert("系统提示", "请先选择要删除的记录！").Show();
            return;
        }
        X.Msg.Confirm("系统提示", "是否删除选中行?", new MessageBoxButtonsConfig
        {
            Yes = new MessageBoxButtonConfig
            {
                Handler = "Ext.net.DirectMethods.DoDelMenu();nodeLoad(TreePanel1.getSelectionModel().getSelectedNode());",
                Text = "是"
            },
            No = new MessageBoxButtonConfig
            {
                Text = "否"
            }
        }).Show();

    }

    [DirectMethod]
    public void DoDelMenu()
    {
        RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
        try
        {
            foreach (SelectedRow row in sm.SelectedRows)
            {
                if (CRMMis.BLL.ERP_Type.GetRecorderCount("PARENT_ID=" + row.RecordID) > 0)
                    throw new Exception("请先删除当前节点的所有子节点");
                CRMMis.BLL.ERP_Type.DeleteFK("Type_ID=" + row.RecordID);
            }
            BuildTreeRoot();
        }
        catch (Exception ex)
        {
            X.Msg.Alert("系统提示", "删除失败|" + ex.Message).Show();
            return;
        }
    }

    #endregion
}